﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using System.Data.SqlClient;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class SuspiciousTransfers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.SuspiciousTransfers_PageTitle;
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            OnlineUser ou = (OnlineUser)Session["User"];

            if ((ou.UserAccessLevel != "5") && (ou.UserAccessLevel != "4"))
            {
                Response.Redirect("~/Default.aspx");
            }
            if (ou.TeamId == "Null")
                Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");
            
            if (!IsPostBack)
            {
                UserIdHF.Value = ou.UserId;
                LanguageHF.Value = ou.Language;
                //ReportDataSourse.SelectParameters.Add("UserId", ou.UserId);
                //ReportDataSourse.SelectParameters.Add("Language", ou.Language);
            }
            
            this.Title = Resources.Resource.SuspiciousReports_PageTitle + " - nivdal.com!";

        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected void SubmitReportAction(object sender, FormViewCommandEventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            if (e.CommandName == "Submit")
            {
                string ReportId = e.CommandArgument.ToString();
                bool DeleteConditionalBid1 = (ReportFormView.FindControl("DeleteConditionalBid1CheckBox") as CheckBox).Checked;
                bool DeleteConditionalBid2 = (ReportFormView.FindControl("DeleteConditionalBid2CheckBox") as CheckBox).Checked;
                bool CancelTransfer = (ReportFormView.FindControl("CancelTransferCheckBox") as CheckBox).Checked;
                bool MarkBuyer1AsCheater = (ReportFormView.FindControl("MarkBuyer1CheaterCheckBox") as CheckBox).Checked;
                bool MarkBuyer2AsCheater = (ReportFormView.FindControl("MarkBuyer2CheaterCheckBox") as CheckBox).Checked;
                bool MarkSellerAsCheater = (ReportFormView.FindControl("MarkSellerCheaterCheckBox") as CheckBox).Checked;
                bool TakeTransferSumFromS = (ReportFormView.FindControl("TakeTransferSumCheckBox") as CheckBox).Checked;
                string Note = (ReportFormView.FindControl("NoteTextBox") as TextBox).Text.Trim();

                SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                SqlCommand storedProcCommand = new SqlCommand("Nivdal_TreatGMTransferReport", myConnection);
                storedProcCommand.CommandType = CommandType.StoredProcedure;
                storedProcCommand.Parameters.AddWithValue("@ReportId", ReportId);
                storedProcCommand.Parameters.AddWithValue("@DeleteConditionalBid1", DeleteConditionalBid1);
                storedProcCommand.Parameters.AddWithValue("@DeleteConditionalBid2", DeleteConditionalBid2);
                storedProcCommand.Parameters.AddWithValue("@CancelTransfer", CancelTransfer);
                storedProcCommand.Parameters.AddWithValue("@MarkB1Cheater", MarkBuyer1AsCheater);
                storedProcCommand.Parameters.AddWithValue("@MarkB2Cheater", MarkBuyer2AsCheater);
                storedProcCommand.Parameters.AddWithValue("@MarkSCheater", MarkSellerAsCheater);
                if ((ReportFormView.FindControl("MessageToSellerCheckBox") as CheckBox).Checked)
                {
                    string MessageToSeller = (ReportFormView.FindControl("MessageToSellerTextBox") as TextBox).Text.Trim();
                    if (MessageToSeller.Length > 0)
                    {
                        storedProcCommand.Parameters.AddWithValue("@MessageToS", MessageToSeller);
                    }
                }
                if ((ReportFormView.FindControl("MessageToBuyer1CheckBox") as CheckBox).Checked)
                {
                    string MessageToBuyer1 = (ReportFormView.FindControl("MessageToBuyer1TextBox") as TextBox).Text.Trim();
                    if (MessageToBuyer1.Length > 0)
                    {
                        storedProcCommand.Parameters.AddWithValue("@MessageToB1", MessageToBuyer1);
                    }
                }
                if ((ReportFormView.FindControl("MessageToBuyer2CheckBox") as CheckBox).Checked)
                {
                    string MessageToBuyer2 = (ReportFormView.FindControl("MessageToBuyer2TextBox") as TextBox).Text.Trim();
                    if (MessageToBuyer2.Length > 0)
                    {
                        storedProcCommand.Parameters.AddWithValue("@MessageToB2", MessageToBuyer2);
                    }
                }
                storedProcCommand.Parameters.AddWithValue("@TakeTransferSumFromS", TakeTransferSumFromS);
                storedProcCommand.Parameters.AddWithValue("@Note", Note);
                storedProcCommand.Parameters.AddWithValue("@TreatedBy", ou.UserId);
                myConnection.Open();

                try
                {
                    storedProcCommand.ExecuteNonQuery();
                }
                finally
                {
                    // Close the connection when done with it.
                    myConnection.Close();
                }

                //ReportDataSourse.DataBind();
                //ReportFormView.DataBind();
                Response.Redirect("~/SuspiciousTransfers.aspx");
            }
            else if (e.CommandName == "Postpone")
            {
                GND.ExecNivdalSQL("ReportId", "Nivdal_PostponeSuspiciousTransferReport", e.CommandArgument);
                ReportDataSourse.DataBind();
                ReportFormView.DataBind();
            }
        }


    }
}